package com.example.a2207_zh.fragment

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Intent
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.a2207_zh.R
import com.example.a2207_zh.adapter.VideoAdapter
import com.example.a2207_zh.database.AppDBUtils
import com.example.a2207_zh.databinding.FragmentVideoBinding
import com.example.a2207_zh.detail.VideoDetailActivity
import com.example.a2207_zh.detail.VideoDetailViewModel
import com.example.a2207_zh.entity.VideoEntity
import com.example.a2207_zh.mvvm.BaseFragment
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.umeng.socialize.ShareAction
import com.umeng.socialize.media.UMVideo


class VideoFragment : BaseFragment<FragmentVideoBinding,VideoViewModel>() {
    val list = mutableListOf<VideoEntity>()
    lateinit var videoAdapter: VideoAdapter
    var page = 1
    var index = 0//记录当前点赞的下标是几--默认为0
    override fun getLayout(): Int  = R.layout.fragment_video

    override fun getViewModel(): Class<VideoViewModel> = VideoViewModel::class.java

    override fun initView() {
        videoAdapter = VideoAdapter(list)
        binding.rvVideo.adapter = videoAdapter
        binding.rvVideo.layoutManager = LinearLayoutManager(context)
        //仿抖音滑动
        val pagerSnapHelper = PagerSnapHelper()
        pagerSnapHelper.attachToRecyclerView(binding.rvVideo)

        viewModel.video(1,10)
        binding.smart.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel.video(page,10)
                binding.smart.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel.video(page,10)
                binding.smart.finishLoadMore()
            }
        })
        //点击item上具体某一个图片
        videoAdapter.addChildClickViewIds(R.id.iv_love,R.id.iv_star,R.id.iv_share,R.id.tv_live)
        videoAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.iv_love->{
                    love(position)
                }
                R.id.iv_star->{
                    star(position)
                }
                R.id.iv_share->{
                    share(position)
                }
                R.id.tv_live->{
                    ARouter.getInstance().build("/app/VideoDetailActivity")
                        .withSerializable("video",list[position])
                        .navigation()

                  /*  val intent = Intent(context,VideoDetailActivity::class.java)
                    intent.putExtra("video",list[position])
                    startActivity(intent)*/
                }
            }
        }

    }

    private fun share(position: Int) {
        val video: UMVideo = UMVideo(list[position].videopath)
        ShareAction(requireActivity()).withMedia(video).open();
    }

    private fun star(position: Int) {
        ToastUtils.showLong("收藏成功")
        val ivStar = videoAdapter.getViewByPosition(position,R.id.iv_star) as ImageView
        ivStar.setImageResource(R.drawable.baseline_star_24)

        AppDBUtils.getDb().getVideoDao().insertVideo(list[position])
    }

    private fun love(position: Int) {
        index = position
        //查看当前是点赞状态还是未点赞状态
        if(list[position].dianzan==1){
            //当前状态为点赞状态----点击后变成未点赞状态
            viewModel.nolove(list[position].id)
        }else{
            viewModel.love(list[position].id)
        }

    }

    override fun initData() {
        viewModel.videoSuccess.observe(this){
            if(it.code==200){
                if(page==1){
                    list.clear()
                }
                list.addAll(it.data)
                videoAdapter.notifyDataSetChanged()
            }
        }
        viewModel.loveSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                //实体类中的值发生变化
                list[index].dianzan = 1
                //图片发生变化
                val ivLove = videoAdapter.getViewByPosition(index,R.id.iv_love) as ImageView
                ivLove.setImageResource(R.drawable.baseline_favorite_24)
                //图片添加动画效果
                val rotate = ObjectAnimator.ofFloat(ivLove,"rotation",-30f,30f,0f)//摇摆动作--从-30到30最后到0
                val alpha = ObjectAnimator.ofFloat(ivLove,"alpha",1f,0f,1f)//透明度
                val scaleX = ObjectAnimator.ofFloat(ivLove,"scaleX",1f,2f,1f)//X轴放大
                val scaleY = ObjectAnimator.ofFloat(ivLove,"scaleY",1f,2f,1f)//X轴放大

                val animatorSet = AnimatorSet()
                animatorSet.setDuration(2000)
                animatorSet.play(rotate).with(alpha).with(scaleX).with(scaleY)
                animatorSet.start()
            }
        }
        viewModel.noloveSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                list[index].dianzan = 0
                val ivLove = videoAdapter.getViewByPosition(index,R.id.iv_love) as ImageView
                ivLove.setImageResource(R.drawable.baseline_favorite_border_24)
            }
        }



    }

}